%delivery rate as a function of link loss precentage for dikjstra,ROMER and
%threshold modified romer

%analyze the threshold function, manipulate its power to find optimal
%threshold power

modified_ROMER_threshold_value = 1.5;
number_of_nodes = 30;
params = ParamsSingleton.instance();

%simulation params
router_type = params.router_type_ROMER;
params.simulation_time_in_msec = 1000;
params.clock_tick_delta_in_msec = 100;

params.ROMER_threshold_power = 2;
params.should_use_constant_forward_probability = true;
params.constant_forward_probability = 1;

packet_size_in_bytes = 500;
number_of_packets_to_deposit_in_each_tick = 500;
num_of_ticks_to_deposit_packets = 10;
ticks_to_deposit_packets = 1:num_of_ticks_to_deposit_packets;

packet_size_in_bytes = 500;
number_of_packets_to_deposit_in_each_tick = 100;
%%%%%%%%%%%%%%%%%%%%%%%%

source_node_id = 26;
dest_node_id = 13;

SingletonInitializer.create_all_singletons();
statistics = StatisticsManager.instance();

loss_rates_vector = 2:2:10;
topology_file_name = ['topology_', num2str(number_of_nodes), '_nodes.mat'];

%simulation statistics
delivery_rates_vector_dikjstra = zeros(1, length(loss_rates_vector));
delivery_rates_vector_romer = zeros(1, length(loss_rates_vector));
delivery_rates_vector_romer_modified = zeros(1, length(loss_rates_vector));

router_type = params.router_type_DIJKSTRA;
for i = 1:length(loss_rates_vector)
     params.link_loss_rate_in_precentage = loss_rates_vector(i);
     
    SingletonInitializer.reinit_all_singletons();
    topology = Topology.read_topology_from_file(topology_file_name, router_type);
    
    statistics = Simulator.run_single_depositor_in_unloaded_network(topology, source_node_id, dest_node_id, packet_size_in_bytes, number_of_packets_to_deposit_in_each_tick, ticks_to_deposit_packets);
    
    delivery_rates_vector_dikjstra(i) = statistics.delivery_rate;
end

router_type = params.router_type_ROMER;
for i = 1:length(loss_rates_vector)
     params.link_loss_rate_in_precentage = loss_rates_vector(i);
     
    SingletonInitializer.reinit_all_singletons();
    topology = Topology.read_topology_from_file(topology_file_name, router_type);
    
    statistics = Simulator.run_single_depositor_in_unloaded_network(topology, source_node_id, dest_node_id, packet_size_in_bytes, number_of_packets_to_deposit_in_each_tick, ticks_to_deposit_packets);
    
    delivery_rates_vector_romer(i) = statistics.delivery_rate;
end

router_type = params.router_type_ROMER;
params.ROMER_threshold_power = modified_ROMER_threshold_value;
for i = 1:length(loss_rates_vector)
     params.link_loss_rate_in_precentage = loss_rates_vector(i);
     
    SingletonInitializer.reinit_all_singletons();
    topology = Topology.read_topology_from_file(topology_file_name, router_type);
    
    statistics = Simulator.run_single_depositor_in_unloaded_network(topology, source_node_id, dest_node_id, packet_size_in_bytes, number_of_packets_to_deposit_in_each_tick, ticks_to_deposit_packets);
    
    delivery_rates_vector_romer_modified(i) = statistics.delivery_rate;
end

% draw the results

figure();
grid minor;
hold on;
xlabel('link loss (%)');
ylabel('delivery rate');
title('delivery rate = f(link loss)');
plot(loss_rates_vector, delivery_rates_vector_dikjstra, 'Color', 'blue');
plot(loss_rates_vector, delivery_rates_vector_romer, 'Color', 'red');
plot(loss_rates_vector, delivery_rates_vector_romer_modified, 'Color', 'magenta');
hold off;